package com.qn.validation.helper;

import com.qn.validation.annotation.InEnumStr;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

/**
 * 字段范围校验器
 *
 * @author QN
 * @date 2024/12/31
 */
public class InEnumStrValidator implements ConstraintValidator<InEnumStr, Object> {

    private InEnumStr annotation;

    @Override
    public void initialize(InEnumStr constraintAnnotation) {
        this.annotation = constraintAnnotation;
    }

    @Override
    public boolean isValid(Object value, ConstraintValidatorContext context) {
        if (value == null) {
            return false;
        }

        Object[] objects = annotation.clazz().getEnumConstants();
        for (Object obj : objects) {
            if (obj.toString().equals(value.toString())) {
                return true;
            }
        }
        return false;
    }

}
